<template>
    <div class="app-container">
      <h4 class="form-header h4">基本信息</h4>
      <el-form ref="userInfo" :model="userInfo" label-width="80px">
        <el-row>
          <el-col :span="8" :offset="2">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="userInfo.nickName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8" :offset="2">
            <el-form-item label="登录账号" prop="userName">
              <el-input  v-model="userInfo.userName" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <h4 class="form-header h4">邮箱信息</h4>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:mail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:mail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:mail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:mail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mailist" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55"  align="center" />
      <el-table-column label="邮箱" align="center" prop="mail" >
        <template slot-scope="scope">
          {{ scope.row.mail + (scope.row.type == 'qq'? '@qq.com':'@163.com')}}
        </template>
      </el-table-column>
      <el-table-column label="授权码" align="center" prop="password" />
      <el-table-column label="发送服务器" align="center" prop="host" />
      <el-table-column label="邮箱类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_mail_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="服务器端口" align="center" prop="port" />
      <el-table-column label="帐号状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_show_hide" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:mail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:mail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改邮件服务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户ID" v-if="false" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="邮箱类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择邮箱类型" @change="changeType">
            <el-option
              v-for="dict in dict.type.sys_user_mail_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="mail">
          <el-input v-model="form.mail" placeholder="请输入邮箱" >
            <template slot="append">{{form.type == 'qq'? '@qq':'@163'}}.com</template>
          </el-input>
        </el-form-item>
        <el-form-item label="授权码" prop="password">
          <el-input v-model="form.password" placeholder="请输入授权码" />
        </el-form-item>
        <el-form-item label="发送服务器" prop="host">
          <el-input v-model="form.host" placeholder="请输入发送服务器" :disabled="true" />
        </el-form-item>
        <el-form-item label="是否验证" prop="auth" v-show="false">
          <el-input v-model="form.auth" placeholder="请输入是否验证" />
        </el-form-item>
        <el-form-item label="服务器端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入服务器端口" />
        </el-form-item>
        <el-form-item label="使用状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_show_hide"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  getMail, delMail, addMail, updateMail } from "@/api/system/mail";
import { userMailsInfo } from "@/api/system/user";

export default {
  name: "Mail",
  dicts: ['sys_user_mail_type', 'sys_show_hide'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      userId: '',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 邮件服务表格数据
      mailist: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        mail: null,
        password: null,
        host: null,
        type: null,
        auth: null,
        port: null,
        status: null,
      },
      // 表单参数
      userInfo: {},
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        mail: [
          { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "授权码不能为空", trigger: "blur" }
        ],
        host: [
          { required: true, message: "发送服务器不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "邮箱类型不能为空", trigger: "change" }
        ],
        auth: [
          { required: true, message: "是否验证不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "服务器端口不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
     this.userId = this.$route.params && this.$route.params.userId;
    if(this.userId){
      this.getList();
    }

  },
  methods: {
    /** 查询邮件服务列表 */
    getList() {
      this.loading = true;
      userMailsInfo(this.userId).then(response => {
        this.userInfo = response.user;
        this.mailist = response.rows;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        mail: null,
        password: null,
        host: 'smtp.qq.com',
        type: 'qq',
        auth: '0',
        port: '465',
        status: '0',
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");

      this.userInfo = {
        nickName: null,
        userName: null,
        userId: null,
      };
      this.resetForm("userInfo");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.userId = this.userId;
      this.open = true;
      this.title = "添加邮件服务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改邮件服务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除邮件服务编号为"' + ids + '"的数据项？').then(function() {
        return delMail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/mail/export', {
        ...this.queryParams
      }, `邮箱配置信息_${new Date().getTime()}.xlsx`)
    },
    /** 根据邮箱类型，生成固定参数*/
    changeType(vlaue){
      if(vlaue === 'qq'){
        this.form.host = 'smtp.qq.com'
      }
      if(vlaue === '163'){
        this.form.host = 'smtp.163.com'
      }

    },
  }
};
</script>
